local KillTips = MClass("KillTips")
local NodePool = custom.NodePool:getInstance()
local Head = require("app.battleV3.ui.objs.KillTipsHead")
local TeamName_Color = cc.c4b(189,255,0, 255)
local EnemyName_Color = cc.c4b(255,1,1, 255)

function KillTips.create(battleUiView)
	local ret = NodePool:getByKey("KillTips")
    if not ret then
        ret =  cc.CSLoader:createNode(Res.KillTips)
    end
    local tips = KillTips.new(ret, battleUiView)
	tips:checkTips()
    return ret, tips
end

function KillTips:checkTips()
	local first = self.battleUiView.killTips[1]
	if not KillTips.process and first then
		KillTips.process = true
		first:performWithDelay(function()
	     	first:free() 
	     	KillTips.process = false
			first:checkTips()
	    end, 2.0)
	end
end

function KillTips:free()
	table.remove(self.battleUiView.killTips, 1)
	self:putToPool()
end

function KillTips:putToPool()
	if not cc.isDead(self.root) then
		self.root:removeFromParent(false)
		NodePool:put(self.root,"KillTips")
	end
end

function KillTips:ctor(battleUiView)
    --self.root 默认为根节点
    --设置事件
    self.head = Head.new(self:findChild("Panel/head_1"))
    self.targetHead = Head.new(self:findChild("Panel/head_2"))
    self.name = self:findChild("Panel/name_1")
    self.targetName = self:findChild("Panel/name_2")
    self.bg = self:findChild("Panel/bg")
    self.text = self:findChild("Panel/text")

    self.assitPanel = self:findChild("Panel_assist")

    self.assitHeads = {}
    self.assitHeads[1] = Head.new(self.assitPanel:findChild("head_1"))
    self.assitHeads[2] = Head.new(self.assitPanel:findChild("head_2"))
    self.assitsName = self.assitPanel:findChild("text")

	self.battleUiView = battleUiView
	table.insert(battleUiView.killTips, self)

    self:init()
end

function KillTips:init()
	self.assitPanel:setVisible(false)
end

function KillTips:setData(data)
	local bEnemy = data.bEnemy
	local assitsNames = data.assitsNames
	
    self.text:loadTexture(Res.KillTips_TEXT[data.continuousKill], 1)

	self.head:setData(data.icon,not bEnemy,false)
	self.targetHead:setData(data.targetIcon,bEnemy,true)

	self.name:setString(data.name)
	self.targetName:setString(data.targetName)

	if bEnemy then
		self.name:setTextColor(TeamName_Color)
		self.targetName:setTextColor(EnemyName_Color)
		self.bg:loadTexture(Res.KillTips_BG_Blue)
	else
		self.targetName:setTextColor(TeamName_Color)
		self.name:setTextColor(EnemyName_Color)
		self.bg:loadTexture(Res.KillTips_BG_Red)
	end

	if #assitsNames > 0 then
		self.assitPanel:setVisible(true)
		local str = L("meleekillNotice0")..assitsNames[1]
		if #assitsNames > 1 then
			str = str..L("meleekillNotice1")
		end
		self.assitsName:setString(str)

		-- if bEnemy then
		-- 	self.assitsName:setTextColor(TeamName_Color)
		-- else
		-- 	self.assitsName:setTextColor(EnemyName_Color)
		-- end
	else
		self.assitPanel:setVisible(false)
	end
end

return KillTips